<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\MyAuth;
use app\admin\model\MySubject;
use app\admin\model\MyTable;
use builder\Builder;
use think\Db;
use think\Request;

class Mymodel
{
    public function index(){
        return view();
    }
    /**添加表格
     * @param Request $request
     * @return \think\response\View
     */
    public function show_list(Request $request){
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $model = $obj->getModel();
        $field = $obj->getField();
        $builder = Builder::table()
            ->setAction('mymodel/page?model_id='.$model_id)
            ->setModelId($model_id)
            ->setMonitorAction('switch','mymodel/change')
            ->setMonitorAction('edit','mymodel/change')
            ->setRightHeader('操作',250);
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'mymodel/del');
            }
            if($value=='add'){
                $builder = $builder->addToolbarDemo('add','添加',[],'mymodel/show_add');
            }
            if($value=='back'){
                $builder = $builder->addToolbarDemo('back','返回');
            }
            if($value=='search'){
                $builder = $builder->addToolbarDemo('search','搜索');
            }
        }
        foreach ($field as $value){
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'mymodel/change');
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'mymodel/change',null,'center','',['开启','关闭']);
                }
            }
        }
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','mymodel/show_edit');
            }
            if($value=='del'){
                $builder->setButton($value,'删除','mymodel/del');
            }
            if($value=='add'){
                $builder->setButton($value,'添加','mymodel/show_add');
            }
            if($value=='detail'){
                $builder->setButton($value,'查看','mymodel/detail');
            }
            if($value=='a'){
                $builder->setButton($value,'编辑字段','myfield/show_list');
            }
        }
        return $builder->fetch();
    }

    /**异步数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $model_id = $request->param('model_id',null);
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $field = $obj->getField();
        $data = Db::name(getTable('models'))
            ->where('type','<>',1)
//            ->paginate($limit)
                ->select();
//            ->toArray();
        foreach ($data as $key=>$value){
            foreach ($field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
            }
            $data[$key] = $value;
        }
        $res['data'] = $data;
        $res['total'] = count($data);
        return json_page_response($res);
    }

    /**加载添加页面
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add(Request $request){
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        return $obj->getForm('mymodel/add');
    }

    /**添加模型
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(10);
        $data = $obj->getData($data);
//        echo '<pre>';
//        print_r($data);
//        exit;
        if(MyTable::addAllTable($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载修改
     * @param Request $request
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        return $obj->getForm('mymodel/edit',$id,true,['table','type','create_time']);
    }


    /**修改模型
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(10);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        $jiu = Db::name($table)->where('id',$data['id'])
            ->find();
        if($data['engine']!==$jiu['engine']){
            $obj->setModelId($data['id']);
            if(!MyTable::changeEngine($obj->getModel('table'),$data['engine'])){
                 return redirect('index/errors',['code'=>205]);
            }

        }
        $jiu = Db::name(getTable('models'))->where('id',$data['id'])
            ->find();
        if($jiu['name']!==$data['name']){
            if($jiu['type']=='联表'){
                Db::name(getTable('jurs'))
                    ->where('name',$jiu['name'])
                    ->update([
                       'name'=>$data['name']
                    ]);
            }
        }
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }


    /**查看详情
     * @param Request $request
     * @return \think\response\View
     */
    public function detail(Request $request){
        $id = $request->param('id');
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        return $obj->getForm('mymodel/edit',$id,false);
    }

    public function del(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('models'))
            ->where('id','in',$id)
            ->select();
        foreach ($data as $value){
            MyTable::delTable($value['table']);
        }
        MyAuth::delTableToLianBiao($id);
        MyTable::delId($id);
        if(Db::name(getTable('models'))->where('id','in',$id)->delete()&&Db::name(getTable('fields'))->where('model_id','in',$id)->delete()){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',205);
        }
    }

    /**修改字段
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $table = $obj->getModel('table');
        $shuju = Db::name($table)
            ->where('id',$data['id'])
            ->find();
        if($shuju[$data['field']]==1){
            $shuju[$data['field']]=0;
        }else{
            $shuju[$data['field']]=1;
        }
        if(Db::name($table)
            ->where('id',$data['id'])
            ->update($shuju)){
            return json_to_respos('修改成功');
        }else{
            return json_to_respos('修改失败',205);
        }
    }
}